home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / ibus / serializable.py < prev    next >
Text File  |  2009-11-05  |  3KB  |  94 lines

  1. # vim:set et sts=4 sw=4:
  2. #
  3. # ibus - The Input Bus
  4. #
  5. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this program; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20. # Boston, MA  02111-1307  USA
  21.  
  22. __all__ = (
  23.         "Serializable",
  24.         "serialize_object",
  25.         "deserialize_object",
  26.     )
  27.  
  28. from object import Object
  29. import dbus
  30. import gobject
  31.  
  32. __serializable_name_dict = dict()
  33.  
  34. def serializable_register(classobj):
  35.     # if not issubclass(classobj, Serializable):
  36.     #     raise "%s is not a sub-class of Serializable" % str(classobj)
  37.     __serializable_name_dict[classobj.__NAME__] = classobj
  38.  
  39. def serialize_object(o):
  40.     if isinstance(o, Serializable):
  41.         l = [o.__NAME__]
  42.         o.serialize(l)
  43.         return dbus.Struct(l)
  44.     else:
  45.         return o
  46.  
  47. def deserialize_object(v):
  48.     if isinstance(v, tuple):
  49.         struct = list(v)
  50.         type_name = struct.pop(0)
  51.         type_class = __serializable_name_dict[type_name]
  52.         o = type_class()
  53.         o.deserialize (struct)
  54.         return o
  55.     return v
  56.  
  57. class SerializableMeta(gobject.GObjectMeta):
  58.     def __init__(cls, name, bases, dict_):
  59.         super(SerializableMeta, cls).__init__(name, bases, dict_)
  60.         if "__NAME__" in cls.__dict__:
  61.             serializable_register(cls)
  62.  
  63. class Serializable(Object):
  64.     __metaclass__ = SerializableMeta
  65.     __gtype_name__ = "PYIBusSerializable"
  66.     __NAME__ = "IBusSerializable"
  67.     def __init__(self):
  68.         super(Serializable, self).__init__()
  69.         self.__attachments = dict()
  70.  
  71.     def set_attachment(self, name, value):
  72.         self.__attachments[name] = value
  73.  
  74.     def get_attachment(self, name):
  75.         return self.__attachments.get(name, None)
  76.  
  77.     def serialize(self, struct):
  78.         d = dbus.Dictionary(signature="sv")
  79.         for k, v in self.__attachments.items():
  80.             d[k] = serialize_object(v)
  81.         struct.append(d)
  82.  
  83.     def deserialize(self, struct):
  84.         d = struct.pop(0)
  85.         self.__attachments = dict()
  86.         for k, v in d.items():
  87.             self.__attachments[k] = deserialize_object(v)
  88.  
  89.     def do_destroy(self):
  90.         self.__attachments = None
  91.         super(Serializable, self).do_destroy()
  92.  
  93. __serializable_name_dict["IBusSerializable"] = Serializable
  94.